// pages/seconds-kill/index.js
var formatTime = require('../../utils/util.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    categories: [{ id: 0, rest: {day: '0', hour: '00', minute: '00', second: '00'}}],
    endTime: {},
    activeCategoryId: 0,
    interval: '',
    secondKillList: [],
    index: 0,
    time: '',
    type: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    if (e) {
      this.setData({
        index: e.index,
        type: e.type
      })
    }

    this.getSecondKillList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //this.dealProduct()
  },

  dealProduct: function () {

    let productList = this.data.secondKillList
    for (var i = 0; i < productList.length; i++) {
      productList[i].pic_url = app.globalData.imgUrl + productList[i].product.file.savepath + productList[i].product.file.savename
      productList[i].product.old_price = res.data.data[0].seckill_product[i].product.old_price.toFixed(2)
    }

    this.showRestTime()
    
    this.setData({
      secondKillList: productList
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.interval)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  tabSourceClick: function (e) {
    this.setData({
      activeCategoryId: e.currentTarget.id
    });

  },

  /**
   * 获取秒杀商品列表
   */
  getSecondKillList: function () {
    let token = wx.getStorageSync('token')
    let that = this
    wx.request({
      url: app.apiUrl('/product/seckilllist'),
      method: 'GET',
      header: {
        "Authorization": 'Bearer ' + token
      },
      data: {

      },
      success: function (res) {
        console.log('getSecondKill')
        console.log(res.data)
        if (res.data.data) {

          wx.setNavigationBarTitle({
            title: res.data.data[0].detail
          })

          let listTemp  = res.data.data
          let productList = listTemp[that.data.index].seckill_product
          for (var i = 0; i < productList.length; i++) {
            productList[i].pic_url = app.globalData.imgUrl + productList[i].product.file.savepath + productList[i].product.file.savename
            productList[i].product.price = res.data.data[0].seckill_product[i].product.price.toFixed(2)
            productList[i].product.old_price = res.data.data[0].seckill_product[i].product.old_price.toFixed(2)
          }
          

          that.setData({
            secondKillList: productList,
            time: that.data.type == 1 ? listTemp[that.data.index].end_at : listTemp[that.data.index].start_at
          })
         
          that.showRestTime(that.data.time)
          
        }


      },
      fail: function (res) {
        console.log(res)
      },
      complete: function (res) {
        // wx.hideToast()
        //console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    let that = this
    clearInterval(that.data.interval)
  },

  showRestTime: function (time) {
    let that = this
    that.data.interval = setInterval(function () {
      let date = new Date(formatTime.formatTime(new Date(time)))
      let restTime = {}
      if (that.data.type == 1)
        restTime = formatTime.restTime(new Date(), date)
      else 
        restTime = formatTime.restTime(new Date(), date)
      let categoriesTemp = that.data.categories
      categoriesTemp[0].rest = restTime
      //console.log(restTime)
      that.setData({
        categories: categoriesTemp
      })

    }, 1000)
  },

  toDetailsTap: function (e) {
    wx.navigateTo({
      url: "/pages/goods-details/index?id=" + e.currentTarget.dataset.id + "&type=1" + "&endtime=" + JSON.stringify(this.data.endTime)
    })
  },

})